import Dep from './dep.js'

export default class Watcher {
  constructor (vm, key, callback) {
    this.vm = vm
    this.key = key
    this.callback = callback

    Dep.target = this
    this.oldValue = vm[key] // 触发 getter, 将当前 watcher 添加到 subs
    Dep.target = null // 防止重复添加
  }
  update () {
    const val = this.vm[this.key]
    if (this.oldValue !== val) {
      this.callback(val)
    }
  }
}
